package com.zw.design.pattern.creational.singleton.hungry.inove;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/***
 * 单例模式 使用反射攻击解决方案
 */
public class TestClass {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class objectClass=HungryLazySingleton2.class;
        Constructor constructor=objectClass.getDeclaredConstructor();
        constructor.setAccessible(true);//设置权限 拿到私有方法
        HungryLazySingleton2 instance = HungryLazySingleton2.getInstance();
        HungryLazySingleton2 newInstance= (HungryLazySingleton2) constructor.newInstance();

        System.out.println("instance = " + instance);
        System.out.println("newInstance = " + newInstance);
        System.out.println(instance==newInstance);
    }
}
